﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace DIPLib
{
    public class PointProcessing
    {
        // Process a grey image only
        public static void negative(byte[] rgbData)
        {


            int len = rgbData.Length;
            for (int i = 0; i < len; i += 4)
            {
                byte greyLevel = rgbData[i];
                rgbData[i] = rgbData[i + 1] = rgbData[i + 2] = rgbData[i + 3] = (byte)(255 - greyLevel);
            }
        }

        public static void thresdholding(byte[] rgbData, byte threshold)
        {

            int len = rgbData.Length;
            for (int i = 0; i < len; i += 4)
            {
                byte greyLevel = rgbData[i];
                byte newGreyLevel;
                if (greyLevel > threshold)
                {
                    newGreyLevel = 255;
                }
                else
                {
                    newGreyLevel = 0;
                }
                rgbData[i] = rgbData[i + 1] = rgbData[i + 2] = rgbData[i + 3] = newGreyLevel;
            }
        }

        public static void logarithmic(byte[] rgbData)
        {
            int len = rgbData.Length;
            for (int i = 0; i < len; i += 4)
            {
                byte greyLevel = rgbData[i];
                byte newGreyLevel = (byte)(Math.Log(1 + (float)greyLevel / 255, 2) * 255);
                rgbData[i] = rgbData[i + 1] = rgbData[i + 2] = rgbData[i + 3] = newGreyLevel;
            }
        }

        public static void powerLaw(byte[] rgbData, float gamma)
        {
            int len = rgbData.Length;
            for (int i = 0; i < len; i += 4)
            {
                byte greyLevel = rgbData[i];
                byte newGreyLevel = (byte)Math.Round(Math.Pow((float)greyLevel / 255, gamma) * 255, 0);
                rgbData[i] = rgbData[i + 1] = rgbData[i + 2] = rgbData[i + 3] = newGreyLevel;
            }
        }
    }
}
